package com.helehousekeeping.xlrequest.utils;

import android.os.Environment;
import android.util.Log;

import com.helehousekeeping.xlrequest.BuildConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by linyiteng on 2015/6/27.
 */
public class MyLog {

    private static final String TAG = "MAPPUSH_STORE";

    public static void debug(Class clazz, String info) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, clazz.getSimpleName() + " ---> " + info);
        }
    }

    public static void info(Class clazz, String info) {
        if (BuildConfig.DEBUG) {
            Log.i(TAG, clazz.getSimpleName() + " ---> " + info);
        }
    }

    public static void warn(Class clazz, String info) {
        if (BuildConfig.DEBUG) {
            Log.w(TAG, clazz.getSimpleName() + " ---> " + info);
        }
    }

    public static void error(Class clazz, Throwable throwable) {
        throwable.printStackTrace();
        if (BuildConfig.DEBUG) {
            Log.e(TAG, clazz.getSimpleName() + " ---> ERROR", throwable);
        }
    }

    public static void saveLog2File(String log) {
        FileWriter fileWriter = null;
        try {
            String fileName = "Young_Server" + ".txt";
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileName);
            fileWriter = new FileWriter(file.getAbsolutePath(), true);

            String finalLog = null;
            if (log.length() > 1024) {
                finalLog = log.substring(0, 1024);
            } else {
                finalLog = log;
            }
            fileWriter.write(finalLog + "\n\n");
        } catch (Exception e) {
            MyLog.error(MyLog.class, e);
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void saveBugLog(String log) {
//        FileWriter fileWriter = null;
//        try {
//            String fileName = "Bug_Response_Server" + ".txt";
//            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileName);
//            fileWriter = new FileWriter(file.getAbsolutePath(), true);
//
//            String finalLog = null;
//            if (log.length() > 1024) {
//                finalLog = log.substring(0, 1024);
//            } else {
//                finalLog = log;
//            }
//
//            fileWriter.write(finalLog + "\n\n");
//        } catch (Exception e) {
//            MyLog.error(MyLog.class, e);
//        } finally {
//            if (fileWriter != null) {
//                try {
//                    fileWriter.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
    }
}
